package com.ruoyi.supplier.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.supplier.domain.ProductSku;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.supplier.domain.bo.ProductAlbumBo;
import com.ruoyi.supplier.domain.vo.ProductAlbumVo;
import com.ruoyi.supplier.domain.ProductAlbum;
import com.ruoyi.supplier.mapper.ProductAlbumMapper;
import com.ruoyi.supplier.service.IProductAlbumService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 商品相册Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-18
 */
@RequiredArgsConstructor
@Service
@DS("slave")
public class ProductAlbumServiceImpl implements IProductAlbumService {

    private final ProductAlbumMapper baseMapper;

    /**
     * 查询商品相册
     */
    @Override
    public ProductAlbumVo queryById(Integer productAlbumId){
        return baseMapper.selectVoById(productAlbumId);
    }

    /**
     * 查询商品相册列表
     */
    @Override
    public TableDataInfo<ProductAlbumVo> queryPageList(ProductAlbumBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ProductAlbum> lqw = buildQueryWrapper(bo);
        Page<ProductAlbumVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商品相册列表
     */
    @Override
    public List<ProductAlbumVo> queryList(ProductAlbumBo bo) {
        LambdaQueryWrapper<ProductAlbum> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ProductAlbum> buildQueryWrapper(ProductAlbumBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ProductAlbum> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getProductId() != null, ProductAlbum::getProductId, bo.getProductId());
        lqw.eq(StringUtils.isNotBlank(bo.getUrl()), ProductAlbum::getUrl, bo.getUrl());
        lqw.eq(bo.getType() != null, ProductAlbum::getType, bo.getType());
        lqw.eq(bo.getMerchantId() != null, ProductAlbum::getMerchantId, bo.getMerchantId());
        return lqw;
    }

    /**
     * 新增商品相册
     */
    @Override
    public Boolean insertByBo(ProductAlbumBo bo) {
        ProductAlbum add = BeanUtil.toBean(bo, ProductAlbum.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setProductAlbumId(add.getProductAlbumId());
        }
        return flag;
    }

    /**
     * 修改商品相册
     */
    @Override
    public Boolean updateByBo(ProductAlbumBo bo) {
        ProductAlbum update = BeanUtil.toBean(bo, ProductAlbum.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ProductAlbum entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除商品相册
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean save(ProductAlbumBo albumBo) {
        if(albumBo.getProductAlbumId() == null){
            return insertByBo(albumBo);
        }else{
            return updateByBo(albumBo);
        }
    }

    @Override
    public Boolean deleteByProductId(Integer productId) {
        QueryWrapper<ProductAlbum> queryWrapper = Wrappers.query();
        queryWrapper.lambda().eq(ProductAlbum::getProductId,productId);
        return baseMapper.delete(queryWrapper) > 0;
    }
}
